package com.dmx.cep.job;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dmx.adi.ADIException;
import com.dmx.adi.ADIFormatException;
import com.dmx.adi.AssetStatusUpdateException;
import com.dmx.adi.PosterNotFoundException;
import com.dmx.adi.VideoNotFoundException;
import com.dmx.cep.entity.AdiImportTask;
import com.dmx.cep.service.ContentService;
import com.dmx.net.ConnectionException;
import com.dmx.net.HostInfoExtractor;
import com.dmx.net.ftp.impl.FtpClient;
import com.dmx.net.ftp.impl.RemoteFtpFileNotExistsException;

/**
 * 
 * @author magicyang
 *	ADI导入后台任务
 */
public class AdiImportJob extends Thread{	
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	private long sleepTime = 4000;
	
	private int maxRecords = 10;
	
	private ContentService contentService;
	
	private String siteId;
	
	private String recomm;
//	private VodContentService  vodRSServiceProxy;
	
	
	
	
	public void run(){
		while(true){
			try{
				Thread.sleep(sleepTime);
				
				List<AdiImportTask> taskList=contentService.getAdiImportTasks(siteId,maxRecords);
				
//				for(AdiImportTask task:taskList){
//					log.info("ADI import begin,task id:"+task.getId());
//					
//					try{
////						AdiParseUtil.adiImport(task.getFtpUrl(),contentService,vodRSServiceProxy,recomm);
//						
//						task.setStatus(1);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("success");
//					}catch(ADIException ae){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("connect.ftp.failed");
//					}catch(ADIFormatException afe){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("adi.format.exception");
//					}catch(AssetStatusUpdateException aue){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("asset.update.error");
//					}catch(VideoNotFoundException ve){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("video.not.found.exception");
//					}catch(PosterNotFoundException ve){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("poster.not.found.exception");
//					}
//					catch(Exception e){
//						task.setStatus(2);
//						task.setLastUpdateTime(new Date());
//						task.setMessage("database.error");
//					}
//					contentService.saveObject(task);
//					log.info("ADI import end,task id:"+task.getId());
//					Thread.sleep(sleepTime);
//				}
				
			}catch(Exception e){
				log.error(e);
			}
		}
	}
	
	private boolean testFtpFileExists(String ftpUrl) throws Exception {
		FtpClient ftpClient = new FtpClient();
		try {
			URL url = null;
			try {
				url = new URL(ftpUrl);
			} catch (MalformedURLException exception) {
				return false;
			}

			HostInfoExtractor hostInfoExtractor = new HostInfoExtractor(url);

			hostInfoExtractor.decorateNetworkProtocolClient(ftpClient);

			try {
				ftpClient.doLogin();
			} catch (Exception e) {

			}

			if (ftpClient.isLogined() == false) {
				return false;
			}

			if (ftpClient.cdToRemoteDir(hostInfoExtractor.extractTheFolder()) == false) {
				return false;
			}
			
			ftpClient.testRemoteFileExists(hostInfoExtractor.extractFileName());


		} catch (ConnectionException ce) {
			return false;

		}  catch(RemoteFtpFileNotExistsException rfe){
			StringBuffer strBuf = new StringBuffer(); 
			return false;
		}
		finally {
			try {
				ftpClient.close();
			} catch (Exception e) {

			}
		}
		return true;
	}

	public long getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}

	public int getMaxRecords() {
		return maxRecords;
	}

	public void setMaxRecords(int maxRecords) {
		this.maxRecords = maxRecords;
	}

	public ContentService getContentService() {
		return contentService;
	}

	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}

	public String getSiteId() {
		return siteId;
	}

	public void setSiteId(String siteId) {
		this.siteId = siteId;
	}
	
	public void setRecomm(String recomm) {
		this.recomm = recomm;
	}


	
}
